/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$('document').ready(function() {
    $("#reg_password").passStrength({
        userid:	"#reg_email"
    });
    $("#reg_email").popover({
        trigger: 'focus',
        placement: 'right',
        template: '<div class="popover"><div class="arrow"></div><div class="popover-inner"><div class="popover-content"><p></p></div></div></div>'
    });
    $("#reg_password").popover({
        trigger: 'focus',
        placement: 'right',
        template: '<div class="popover"><div class="arrow"></div><div class="popover-inner"><div class="popover-content"><p></p></div></div></div>'
    });
    $("#reg_repassword").popover({
        trigger: 'focus',
        placement: 'right',
        template: '<div class="popover"><div class="arrow"></div><div class="popover-inner"><div class="popover-content"><p></p></div></div></div>'
    });
     $("#reg_captcha").popover({
        trigger: 'focus',
        placement: 'right',
        template: '<div class="popover"><div class="arrow"></div><div class="popover-inner"><div class="popover-content"><p></p></div></div></div>'
    });
    $('#register_btn').click(function() {
        return verify_captcha_reg(ajax_reg_submit, function(){return false;});
    });
    
    $("#reg_email").focus();
});

// 检查各字段是否填写正确
function check_reg(reg_email, reg_password, reg_repassword) {
    var ifChecked = true;
    
    $('div.control-group:has(#reg_email)').removeClass('error');
    $('div.control-group:has(#reg_password)').removeClass('error');
    $('div.control-group:has(#reg_repassword)').removeClass('error');
    $('#reg_email').nextAll('span').remove();
    $('#reg_password').nextAll('span').remove();
    $('#reg_repassword').nextAll('span').remove();
    
    //清除错误信息
    
    if (reg_email === "") {
        var $reg_email = $('#reg_email');
        // 添加错误信息
        $reg_email.after('<span class=\'help-inline\'>邮箱不能为空！</span>');
        // 添加error class，更新界面
        $('div.control-group:has(#reg_email)').addClass('error');

        ifChecked = false;
        $reg_email.focus();
    } else {
        if (!EB.util.is_email(reg_email)) {
            var $reg_email = $('#reg_email');            
            $reg_email.after('<span class=\'help-inline\'>邮箱格式不合法！</span>');
            $('div.control-group:has(#reg_email)').addClass('error');

            ifChecked = false;
            $reg_email.select();
        }
    }

    if (reg_password.length < 6) {
        var $reg_password = $('#reg_password');
        $reg_password.after('<span class=\'help-inline\'>密码长度不得少于6位！</span>');
        $('div.control-group:has(#reg_password)').addClass('error');

        ifChecked = false;
        $reg_password.val('');
        $('#reg_repassword').val('');
    }

    if (reg_password !== reg_repassword) {
        var $reg_repassword = $('#reg_repassword');
        $reg_repassword.after('<span class=\'help-inline\'>密码输入不一致！</span>');
        $('div.control-group:has(#reg_repassword)').addClass('error');

        ifChecked = false;
        $('#reg_password').val('');
        $reg_repassword.val('');
    }

    return ifChecked;
}

function ajax_reg_submit() {
	var reg_email = $('#reg_email').val();
    var reg_password = $('#reg_password').val();
    var reg_repassword = $('#reg_repassword').val();
    
    $('div.control-group:has(#reg_agree)').removeClass('error');
    $('#reg_agree_label').nextAll('span').remove();
    if ($("#reg_agree").attr('checked') == undefined) {
    	$('div.control-group:has(#reg_agree)').addClass('error');
    	$('#reg_agree_label').after('<span class=\'help-inline\'>请您阅读并同意网站协议</span>');
    	return false;
    }
    else {
    	if (check_reg(reg_email, reg_password, reg_repassword) === true) {
            var salt = genSalt();
            var pw_encrypt = encrypt(encrypt(reg_password) + salt);
            $.post("register/ajax_register", {
                email: reg_email,
                password: pw_encrypt + salt
            }, function(data, textStatus) {
            	if (data.code == 1) {
            		location.href = (window.EB_DEV ? "/ourplayground" : "") + "/register/register_success";
            	}
            	else if (data.code == -1) {
            		var $reg_email = $('#reg_email');
            		$reg_email.after('<span class=\'help-inline\'>该邮箱地址已经被使用！</span>');
                    $('div.control-group:has(#reg_email)').addClass('error');
            	}
            	else {
            		alert("未知错误" + data.code + "，请将此情况告知眼缘网的管理员，谢谢。")
            	}
            });
        	return false;
        }
    	return false;    	
    }
}

function verify_captcha_reg(match_function, unmatch_function) {
    $.post("security/verify_captcha", {
        captcha: $('#reg_captcha').val()
    }, function(data, textStatus) {
        $("#reg_captcha").nextAll('span').remove();
        $('div.control-group:has(#reg_captcha)').removeClass('error');
        if (data.match == 1) {
            match_function();
        } else {
            $("#reg_captcha").after('<span class=\'help-inline\'>请输入正确的验证码</span>');
            $('div.control-group:has(#reg_captcha)').addClass('error');
            unmatch_function();
        }
    }, "json");
    return false;
}